import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.12
import "storage.js" as Storage

Item {
    id: root
    width: 850
    height: 580

    property real minSize:  Math.min(width/850, height/580)
    signal qmlSN_sqlWriteFinish()
    onQmlSN_sqlWriteFinish: {
        var t_var = Storage.readWhichChannelRunning()
        eXC_Gauge.circularGaugeValue = 0.01*$sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"机端电压")
        eXC_Gauge2.circularGaugeValue = 0.01*$sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"励磁电流")
        eXC_Gauge.text_value = $sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"机端电压")*0.0001
                *$sqlMcRecorder.qml_getFactor("stator_voltage")
        eXC_Gauge2.text_value = $sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"励磁电流")*0.0001
                *$sqlMcRecorder.qml_getFactor("exc_current")
    }

    Text {
        id: title
        x: 78*root.minSize
        y: 70*root.minSize
        width: 80*root.minSize
        height: 216*root.minSize
        color: "#acdd84"
        text: qsTr("空载\n特性\n试验")
        font.bold: true
        font.pixelSize: 40*root.minSize
    }

    EXC_Gauge3 {
        id: eXC_Gauge
        x: 611*root.minSize
        y: 308*root.minSize
        width: 180*root.minSize
        element_cnmsgText: "定子电压 自动给定"
    }

    EXC_Gauge3 {
        id: eXC_Gauge2
        x: 611*root.minSize
        y: 22*root.minSize
        width: 180*root.minSize
        element_cnmsgText: "励磁电流 手动给定"
        element_numsizeText: "A"
        element_tipText: "If(%)"
    }


    Item {
        x: 70*root.minSize
        y: 320*root.minSize
        width: 450*root.minSize
        height: 250*root.minSize

        Rectangle
        {
            id:p95_toobarHeader
            height: 25*root.minSize
            width:parent.width
            radius: height/4
            color: "#bb5ed8b8"

            Label {
                id:label_num
                anchors.left: parent.left
                anchors.right: ts1.left
                anchors.verticalCenter: parent.verticalCenter
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
            }
            ToolSeparator {
                id:ts1
                x:parent.width*0.1
                height: parent.height
                MouseArea{
                    height: parent.height
                    anchors.leftMargin: -width/2+parent.width/2
                    width: 50*root.minSize
                    anchors.left: parent.left
                    acceptedButtons: Qt.LeftButton //只处理鼠标左键
                    onPositionChanged: {
                        parent.x = mouseX+parent.x-width/2
                    }
                }
            }
            Label {
                id:label_time
                text: qsTr("时间")
                anchors.left: ts1.right
                anchors.right: ts2.left
                font.bold: true
                font.family: "Tahoma"
                color: "white"
                font.pixelSize: 17*root.minSize
                anchors.verticalCenter: parent.verticalCenter
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
            }
            ToolSeparator {
                id:ts2
                x:parent.width*0.5
                height: parent.height
                MouseArea{
                    height: parent.height
                    anchors.leftMargin: -width/2+parent.width/2
                    width: 50
                    anchors.left: parent.left
                    acceptedButtons: Qt.LeftButton //只处理鼠标左键
                    onPositionChanged: {
                        parent.x = mouseX+parent.x-width/2
                    }
                }
            }
            Label {
                id:label_ug
                text: qsTr("机端电压")
                anchors.left: ts2.right
                anchors.right: ts3.left
                font.bold: true
                font.family: "Tahoma"
                color: "white"
                font.pixelSize: 17*root.minSize
                anchors.verticalCenter: parent.verticalCenter
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
            }
            ToolSeparator {
                id:ts3
                x:parent.width*0.75
                height: parent.height
                MouseArea{
                    height: parent.height
                    anchors.leftMargin: -width/2+parent.width/2
                    width: 50
                    anchors.left: parent.left
                    acceptedButtons: Qt.LeftButton //只处理鼠标左键
                    onPositionChanged: {
                        parent.x = mouseX+parent.x-width/2
                    }
                }
            }
            Label {
                id:label_il
                text: qsTr("励磁电流")
                anchors.left: ts3.right
                anchors.right: parent.right
                font.bold: true
                font.family: "Tahoma"
                color: "white"
                font.pixelSize: 17*root.minSize
                anchors.verticalCenter: parent.verticalCenter
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
            }

        }


        ListView {
            id: p95_listView
            clip: true
            width: parent.width
            anchors.top: p95_toobarHeader.bottom
            anchors.bottom: parent.bottom
            snapMode: ListView.SnapToItem
            orientation:ListView.Vertical

            ScrollBar.vertical: ScrollBar {
                width: 25*root.minSize
                active: true
                anchors.right: parent.right
                parent: parent
            }

            model:ListModel{
                id:p95_listView_model
            }

            delegate:Item {
                id: wrapper
                width: parent.width
                height: 30*root.minSize

                EXC_listViewItemDelegate2{
                    id: p95_listViewItemdeleg_num
                    locationBase: label_num
                    mtext: text_num
                    textFontpointSize: 13*root.minSize
                }
                EXC_listViewItemDelegate2{
                    id: p95_listViewItemdeleg_date
                    locationBase: label_time
                    mtext: text_date
                    textFontpointSize: 13*root.minSize
                }
                EXC_listViewItemDelegate2{
                    id: p95_listViewItemdeleg_ug
                    locationBase: label_ug
                    mtext: text_ug
                    textFontpointSize: 13*root.minSize
                }
                EXC_listViewItemDelegate2{
                    id: p95_listViewItemdeleg_il
                    locationBase: label_il
                    mtext: text_il
                    textFontpointSize: 13*root.minSize
                }

                MouseArea {
                    id:mouseAreaThis
                    anchors.fill: parent
                    onClicked: p95_listView .currentIndex = index
                    Rectangle{
                        anchors.fill: parent
                        border.width:1
                        border.color:wrapper.ListView.isCurrentItem?"lightskyblue":"transparent"

                        color:"transparent"
                    }
                }
            }

            function addModel(time, ug, il)
            {
                p95_listView_model.append({
                                              "text_num":p95_listView.count+1,
                                              "text_date":time,
                                              "text_ug":ug,
                                              "text_il":il
                                          });
            }
        }
    }

    EXC_Normal_BtnUncheckStyle {
        id: eXC_Normal_BtnUncheckStyle
        x: 278*root.minSize
        y: 143*root.minSize
        width: 190*root.minSize
        height: 70*root.minSize
        nameFontpixelSize: 30*root.minSize
        nameText: qsTr("记录数据点")
        onClicked: {
            var t_date = new Date();
            var t_var = Storage.readWhichChannelRunning()
            var t_ug = $sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"机端电压")*0.0001
                            *$sqlMcRecorder.qml_getFactor("stator_voltage");
            var t_il = $sqlMcRecorder.qml_getRecordVal_bystr_toint16(t_var+"励磁电流")*0.0001
                            *$sqlMcRecorder.qml_getFactor("exc_current");
            p95_listView.addModel(t_date.toLocaleString(Qt.locale(), "MM-dd hh:mm:ss.zzz"), String(t_ug), String(t_il))
        }
    }

    EXC_Normal_BtnUncheckStyle {
        id: eXC_Normal_BtnUncheckStyle1
        x: 278*root.minSize
        y: 47*root.minSize
        width: 190*root.minSize
        height: 70*root.minSize
        nameFontpixelSize: 30*root.minSize
        nameText: qsTr("清除选定行")
        onClicked: {
            if(p95_listView.count>0)
                p95_listView_model.remove(p95_listView.currentIndex)
            for(var i=0; i < p95_listView.count; i++)
                p95_listView_model.setProperty(i, "text_num", i+1)
        }
    }

    EXC_Normal_BtnUncheckStyle {
        id: eXC_Normal_BtnUncheckStyle2
        x: 278*root.minSize
        y: 230*root.minSize
        width: 190*root.minSize
        height: 70*root.minSize
        nameFontpixelSize: 30*root.minSize
        nameText: qsTr("导出数据")
        onClicked:
        {
            var t_var = []
            t_var[0]=String("")+","
                    +String("时间")+","
                    +String("机端电压")+","
                    +String("励磁电流")
            for(var t=0; t<p95_listView_model.count; t++)
                t_var[t+1]=String(p95_listView_model.get(t).text_num)+","
                        +String(p95_listView_model.get(t).text_date)+","
                        +String(p95_listView_model.get(t).text_ug)+","
                        +String(p95_listView_model.get(t).text_il)
            excRecordDisplayer.sl_selectCvs(t_var)
        }
    }

}
